//
//  MSTileMap.m
//  MagicSword
//
//  Created by Netease on 15/11/20.
//  Copyright © 2015年 Jonear. All rights reserved.
//

#import "MSTileMap.h"

@implementation MSTileMap

-(id)initWithIndex:(NSInteger)index dot:(NSInteger)dot
{
    self = [super initWithMapNamed:[NSString stringWithFormat:@"map%d_%d.tmx", index+1, dot+1]];
    if (self)
    {
        _road = [self layerNamed:@"road"];
        _item = [self layerNamed:@"item"];
        _enemy = [self layerNamed:@"enemy"];
        _door = [self layerNamed:@"door"];
        _npc = [self layerNamed:@"npc"];
        _other = [self layerNamed:@"other"];
        _hero = [self layerNamed:@"hero"];
        _wall = [self layerNamed:@"wall"];
        
        _enemyArray = [NSMutableArray array];
        _heroPointArray = [NSMutableArray array];
    }
    [self titledMapAnalytic];
    return self;
}

-(void)titledMapAnalytic
{
    for (int x = 0; x <= self.mapSize.width; x++)
    {
        for (int y = 0; y <= self.mapSize.height; y++)
        {
            CGPoint towerLoc = CGPointMake(x, y);
            
            // 敌人
            int enemy_tileGid = [_enemy tileGidAt:towerLoc];
            
            if (enemy_tileGid) {
                NSDictionary *props = [self propertiesForGid:enemy_tileGid];
                NSString *value = [props valueForKey:@"type"];
                
                if (value.length > 0) {
                    [_enemyArray addObject:[MSEnemyInfo enemyWithType:[value intValue] point:towerLoc]];
                }
                
                SKSpriteNode *node = [_enemy tileAt:towerLoc];
                [node removeFromParent];
            }
            
            
            // 英雄
            int heroPoint_tileGid = [_hero tileGidAt:towerLoc];
            if (heroPoint_tileGid) {
                
                SKSpriteNode *node = [_hero tileAt:towerLoc];
                [node removeFromParent];
                
                [_heroPointArray addObject:[NSValue valueWithCGPoint:towerLoc]];
            }
        }
    }
//    NSLog(@"英雄初始化位置%f,%f", self.down.x, self.down.y);
    return;
}

-(void)openDoorWithType:(int)type
{
    for (int x = 0; x <= self.mapSize.width; x++)
    {
        for (int y = 0; y <= self.mapSize.height; y++)
        {
            CGPoint towerLoc = CGPointMake(x, y);
            
            // 敌人
            int door_tileGid = [_door tileGidAt:towerLoc];
            
            if (door_tileGid) {
                NSDictionary *props = [self propertiesForGid:door_tileGid];
                NSString *value = [props valueForKey:@"type"];
                
                if (value.length > 0 && [value intValue]==type) {
//                    SKSpriteNode *node = [_door tileAt:towerLoc];
//                    [node removeFromParent];
                    [_door removeTileAtCoord:[_door coordForPoint:towerLoc]];
                    return;
                }
            }
        }
    }
}

@end
